//
//  HWScanLoginController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/11/2.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWScanLoginController: HWBaseViewController {
    var closeButton:UIButton!
    var centerImageView:UIImageView!
    var centerLabel:UILabel!
    var loginButton:UIButton!
    var cancelButton:UIButton!
    var httpManager = HWHomeHttpManager()
    var strScanned = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
    }
}

extension HWScanLoginController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        
        closeButton = UIButton(type: .custom)
        closeButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        closeButton.setTitle("关闭", for: .normal)
        closeButton.titleLabel?.font = .systemFont(ofSize: 14*widthScale)
        closeButton.addTarget(self, action: #selector(closeButtonClick(_:)), for: .touchUpInside)
        view.addSubview(closeButton)
        
        centerImageView = UIImageView()
        centerImageView.contentMode = .scaleAspectFill
        centerImageView.image = UIImage(named: "scanLoginImage")
        view.addSubview(centerImageView)
        
        centerLabel = HWLabel.createBoldLabel(title: "网页登录确认", color: "#333333", font: 16*widthScale)
        view.addSubview(centerLabel)
        
        loginButton = UIButton(type: .custom)
        loginButton.titleLabel?.font = .boldSystemFont(ofSize: 16*widthScale)
        loginButton.frame = CGRect(x: 0, y: 0, width: 303*widthScale, height: 48*widthScale)
        loginButton.layer.cornerRadius = 24*widthScale
        loginButton.layer.masksToBounds = true
        loginButton.setTitle("登录", for: .normal)
        loginButton.addTarget(self, action: #selector(loginButtonClick(_:)), for: .touchUpInside)
        view.addSubview(loginButton)
        UIColor.gradientColor(view: loginButton, colors: [UIColor.colorWithHexString(hexString: "#FA8C08").cgColor,
                                                          UIColor.colorWithHexString(hexString: "#FDC013").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        cancelButton = UIButton(type: .custom)
        cancelButton.setTitle("取消登录", for: .normal)
        cancelButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        cancelButton.titleLabel?.font = .systemFont(ofSize: 16*widthScale)
        cancelButton.addTarget(self, action: #selector(closeButtonClick(_:)), for: .touchUpInside)
        view.addSubview(cancelButton)
    }
    
    private func initFrame() -> Void {
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(58*widthScale)
            make.height.equalTo(20*widthScale)
        }
        
        centerImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(192*widthScale)
            make.width.height.equalTo(109*widthScale)
        }
        
        centerLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(centerImageView.snp.bottom).offset(18*widthScale)
            make.height.equalTo(23*widthScale)
        }
        
        loginButton.snp.makeConstraints { make in
            make.left.equalTo(36*widthScale)
            make.right.equalTo(-36*widthScale)
            make.height.equalTo(48*widthScale)
            make.top.equalTo(centerLabel.snp.bottom).offset(249*widthScale)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(loginButton.snp.bottom).offset(31*widthScale)
            make.height.equalTo(23*widthScale)
        }
    }
}

extension HWScanLoginController {
    
}

extension HWScanLoginController {
    @objc func closeButtonClick(_:UIButton) -> Void {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func loginButtonClick(_:UIButton) -> Void {
        self.view.showWaitHUD(title: "加载中")
        self.httpManager.scanLoginAuth(token: self.strScanned) { success in
            self.view.showHUD(title: "登录成功")
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
                self.navigationController?.popViewController(animated: true)
            }
        } failure: { error in
            self.view.showHUD(title: error)
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
                self.navigationController?.popViewController(animated: true)
            }
        }

    }
}
